package com.banmao.mall.controller.client;

import com.banmao.mall.model.bo.client.collect.AddorDeleteBO;
import com.banmao.mall.model.bo.client.collect.CollectListBO;
import com.banmao.mall.model.vo.BaseRespVO;
import com.banmao.mall.model.vo.client.collect.AddorDeleteVO;
import com.banmao.mall.model.vo.client.collect.CollectListVO;
import com.banmao.mall.service.client.CollectService;
import com.banmao.mall.util.CustomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController("cCollectController")
@RequestMapping("/wx")
public class CollectController {

    @Autowired
    CollectService collectService;

    // 用户收藏列表
    @RequestMapping("/collect/list")
    public BaseRespVO collectList(@Validated CollectListBO collectListBO) throws IllegalAccessException {

        // 获取userId
        Integer userId = CustomUtils.judgeUserLogin();

        collectListBO.setUserId(userId);

        CollectListVO collectListVO = collectService.collectList(collectListBO);
        if(collectListVO == null) {
            return BaseRespVO.error("服务器抽风啦", 401);
        }

        return BaseRespVO.ok(collectListVO);
    }


    // 收藏或者取消收藏
    @RequestMapping("/collect/addordelete")
    public BaseRespVO collectAddorDelete(@RequestBody AddorDeleteBO addorDeleteBO) throws IllegalAccessException {

        Integer userId = CustomUtils.judgeUserLogin();
        addorDeleteBO.setUserId(userId);

        AddorDeleteVO addorDeleteVO = null;
        try {
            addorDeleteVO = collectService.collectAddorDelete(addorDeleteBO);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVO.error("收藏失败啦", 401);
        }

        return BaseRespVO.ok(addorDeleteVO);
    }

}
